package com.ywu.file.sys.standard.api;

import com.ywu.boot.context.YwuContext;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.common.result.CommonResult;
import com.ywu.file.sys.standard.dto.YwuFileSysDto;
import com.ywu.file.sys.standard.service.YwuFileSysServiceV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName YwuFileSysApi
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/8/2 21:49
 * @Version 1.0
 **/
@RestController
@RequestMapping("/ywu/file/sys/v2")
public class YwuFileSysV2Api {
    @Autowired
    private YwuFileSysServiceV2 service;

    /**
    * @Author GroundDemo
    * @Description 分页查询文件数据列表
    * @Date 10:52 2024/8/3
    * @Param [ywuFileSysDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/ywu.file.sys.query.page.list")
    public CommonResult<Object> queryFilePageList(@RequestBody YwuFileSysDto ywuFileSysDto) {
        return service.queryFilePageList(ywuFileSysDto);
    }

    /**
    * @Author GroundDemo
    * @Description 文件上传
    * @Date 20:16 2024/8/9
    * @Param []
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/ywu.file.sys.upload")
    public CommonResult<Object> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam(value = "isShare", required = false) Boolean isShare) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        YwuFileSysDto ywuFileSysDto = new YwuFileSysDto();
        ywuFileSysDto.setIsShare(isShare);
        return service.uploadFile(file, ywuFileSysDto, currentContext);
    }

    /**
    * @Author GroundDemo
    * @Description 删除文件
    * @Date 22:04 2024/8/9
    * @Param [ywuFileSysDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/ywu.file.sys.delete")
    public CommonResult<Object> deleteFile(@RequestBody YwuFileSysDto ywuFileSysDto) {
        return service.deleteFile(ywuFileSysDto);
    }

    /**
    * @Author GroundDemo
    * @Description 下载文件
    * @Date 16:25 2024/8/10
    * @Param []
    * @return void
    **/
    @GetMapping("/ywu.file.sys.download.get")
    public void downloadFileGet(HttpServletResponse response, @RequestParam("fileId") String fileId) {
        YwuFileSysDto ywuFileSysDto = new YwuFileSysDto();
        ywuFileSysDto.setFileId(fileId);
        service.downloadFile(response, ywuFileSysDto);
    }

    /**
     * @Author GroundDemo
     * @Description 下载文件
     * @Date 16:25 2024/8/10
     * @Param []
     * @return void
     **/
    @GetMapping("/ywu.file.sys.download/{fileId}")
    public void downloadFile(HttpServletResponse response, @PathVariable("fileId") String fileId, @RequestParam(required = false) String type) {
        YwuFileSysDto ywuFileSysDto = new YwuFileSysDto();
        ywuFileSysDto.setFileId(fileId);
        ywuFileSysDto.setType(type);
        service.downloadFile(response, ywuFileSysDto);
    }

    /**
    * @Author GroundDemo
    * @Description 根据文件ID修改是否共享
    * @Date 18:53 2024/8/11
    * @Param [ywuFileSysDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/ywu.file.sys.update.isShare")
    public CommonResult<Object> updateIsShareByFileId(@RequestBody YwuFileSysDto ywuFileSysDto) {
        return service.updateIsShareByFileId(ywuFileSysDto);
    }

    /**
     * @Author GroundDemo
     * @Description 文件上传 - 分段式
     * @Date 20:16 2024/8/9
     * @Param []
     * @return com.ywu.common.result.CommonResult<java.lang.Object>
     **/
    @PostMapping("/ywu.file.sys.upload.multi")
    public CommonResult<Object> uploadFileMulti(@RequestParam("file") MultipartFile file, @RequestParam("taskId") String taskId,
                                                @RequestParam("segmentId") String segmentId) {
        return service.uploadFileMulti(file, taskId, segmentId);
    }

    /**
    * @Author GroundDemo
    * @Description 分段式文件上传完成后，校验所有分段是否全部上传完毕
    * @Date 11:32 2024/9/16
    * @Param [taskId]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/ywu.file.sys.upload.multi.segment.check")
    public CommonResult<Object> uploadFileMultiSegmentCheck(@RequestBody YwuFileSysDto ywuFileSysDto) {
        return service.uploadFileMultiSegmentCheck(ywuFileSysDto);
    }

    /**
    * @Author GroundDemo
    * @Description 分段式文件上传合并最终文件
    * @Date 12:18 2024/9/16
    * @Param [ywuFileSysDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/ywu.file.sys.upload.multi.merge")
    public CommonResult<Object> uploadFileMultiMerge(@RequestBody YwuFileSysDto ywuFileSysDto) {
        return service.uploadFileMultiMerge(ywuFileSysDto);
    }
}
